define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'user/user/index',
                    add_url: 'user/user/add',
                    edit_url: 'user/user/edit',
                    del_url: Config.admin.nickname == 'admin' ? 'user/user/del' : '',
                    multi_url: 'user/user/multi',
                    table: 'user',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'user.id',
                pageSize: 10,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), sortable: true},
                        {field: 'nickname', title: __('Nickname'), operate: 'LIKE'},
                        {field: 'mobile', title: __('Mobile'), operate: 'LIKE'},
                        {field: 'avatar', title: __('Avatar'), events: Table.api.events.image, formatter: Table.api.formatter.image, operate: false},
                        {field: 'invite_user_id', title: __('邀请人')},
                        {field: 'rose_money', title: __('rouse币'), operate: 'BETWEEN', sortable: true},
                        {field: 'gender', title: __('性别'), sortable: true, formatter: Table.api.formatter.gender,searchList: {'1': '男', '0': '未知','2':'女'},formatter:function (value, row, index) {
                            if(value == 0){
                                return '未知';
                            }else if(value == 1){
                                return '男';
                            }else{
                                return '女';
                            }
                        }},
                        {field: 'birthday', title: __('生日'), sortable: true,formatter: Table.api.formatter.birthday, operate: false, addclass: 'datetimerange',},
                        {field: 'login_source', title: __('来源'), sortable: true,formatter: Table.api.formatter.login_source,searchList: {'xcx': 'xcx', 'ios': 'ios','android':'android'}},
                        // {field: 'last_login_time', title: __('Logintime'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                        //{field: 'loginip', title: __('Loginip'), formatter: Table.api.formatter.search},
                        {field: 'add_time', title: __('Jointime'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                        //{field: 'joinip', title: __('Joinip'), formatter: Table.api.formatter.search},
                        {field: 'status', title: __('Status'), formatter: Table.api.formatter.status, searchList: {'1': '正常', '0': '冻结','-1':'注销'}},
                        {field: 'is_supervise', title: __('是否监管'), formatter: Table.api.formatter.status, searchList: {'1': '是', '0': '否'}},
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate,
                            buttons: [
                                
                                {
                                    name: '余额充值',
                                    text: __('余额充值'),
                                    classname: 'btn btn-info btn-xs btn-detail btn-dialog',
                                    url: 'user/user/balance',
                                    visible: function (row) {
                                        if(Config.admin.id==1){
                                            return true;
                                        }else {
                                            return false;
                                        }
                                    },
                                },
                                {
                                    name: '财务记录',
                                    text: __('财务记录'),
                                    classname: 'btn btn-info btn-xs btn-detail btn-dialog',
                                    url: 'user/user/finance'
                                }, {
                                    name: '地址管理',
                                    text: __('地址管理'),
                                    classname: 'btn btn-info btn-xs btn-detail btn-dialog',
                                    url: 'user/user/address'
                                }, {
                                    name: '产品管理',
                                    text: __('产品管理'),
                                    classname: 'btn btn-info btn-xs btn-detail btn-dialog',
                                    url: 'user/user/product'
                                }, {
                                    name: '订单管理',
                                    text: __('订单管理'),
                                    classname: 'btn btn-info btn-xs btn-detail btn-dialog',
                                    url: 'user/user/order'
                                },{
                                    name: 'status',
                                    title: '开始监管',
                                    text: '开始监管',
                                    confirm: '是否确定开始监管该用户？',
                                    classname: 'btn btn-danger btn-xs btn-magic btn-ajax',
                                    url: function (row) {
                                        return 'user/user/setSupervise?is_supervise=1&id='+row.id;
                                    },
                                    visible: function (row) {
                                        if(row.is_supervise==1){
                                            return false;
                                        }else {
                                            return true;
                                        }
                                    },
                                    success: function (res){
                                        table.bootstrapTable('refresh');
                                    }
                                },{
                                    name: 'status',
                                    title: '取消监管',
                                    text: '取消监管',
                                    confirm: '是否确定取消监管该用户？',
                                    classname: 'btn btn-success btn-xs btn-magic btn-ajax',
                                    url: function (row) {
                                        return 'user/user/setSupervise?is_supervise=0&id='+row.id;
                                    },
                                    visible: function (row) {
                                        if(row.is_supervise==1){
                                            return true;
                                        }else {
                                            return false;
                                        }
                                    },
                                    success: function (res){
                                        table.bootstrapTable('refresh');
                                    }
                                }
                            ]
                            
                        }
                    ]
                ]
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        balance: function () {
            Controller.api.bindevent();
        },
        finance: function () {
             // 初始化表格参数配置
             Table.api.init({
                extend: {
                    index_url: 'user/user/finance?ids='+ Config.ids,
                    table: 'user',
                }
            });
            var table = $("#table");
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {field: 'id', title: __('Id'), sortable: true},
                        {field: 'title', title: __('说明'), operate: 'LIKE'},
                        {field: 'rose_money', title: __('积分变动'), operate: 'LIKE'},
                        {field: 'add_time', title: __('Jointime'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                    ]
                ]
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        address: function () {
            // 初始化表格参数配置
            Table.api.init({
               extend: {
                   index_url: 'user/user/address?ids='+ Config.ids,
                   table: 'user',
               }
           });
           var table = $("#table");
           // 初始化表格
           table.bootstrapTable({
               url: $.fn.bootstrapTable.defaults.extend.index_url,
               pk: 'id',
               sortName: 'id',
               columns: [
                   [
                       {field: 'id', title: __('Id'), sortable: true},
                       {field: 'name', title: __('收货人姓名'), operate: 'LIKE'},
                       {field: 'address', title: __('收货地址'), operate: 'LIKE', formatter:function (value, row, index) {
                            return row.province+row.city+row.district+row.address;
                        }},
                        {field: 'is_default', title: __('是否为默认地址'), formatter: Table.api.formatter.status, searchList: {'1': '是', '0': '否'}},
                       {field: 'add_time', title: __('Jointime'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                   ]
               ]
           });
           // 为表格绑定事件
           Table.api.bindevent(table);
       },
       product: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'user/user/product?ids='+ Config.ids,
                    table: 'user',
                }
            });
            var table = $("#table");
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {field: 'id', title: __('Id')},
                        {field: 'category', title: __('分类'), operate: false, searchList: Config.searchList, formatter: Table.api.formatter.label},
                        {field: 'title', title: __('标题')},
                        {field: 'video_img', title: __('视频封面'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'price', title: __('积分'), operate: false},
                        {field: 'weight', title: __('重量'), operate: false},
                        {field: 'status', title: __('审核状态'), formatter: Table.api.formatter.status, searchList: {'0': '待审核', '1': '已上架','-1':'已下架'},width: 100},
                        {field: 'is_sale', title: __('交易状态'), formatter: Table.api.formatter.status, searchList: {'0': '未售出', '1': '已售出'},width: 100},
                        {field: 'add_time', title: __('创建时间'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                    ]
                ]
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        order: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'user/user/order?ids='+ Config.ids,
                    table: 'user',
                }
            });
            var table = $("#table");
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {field: 'id', title: __('Id'), sortable: true},
                        {field: 'order_id', title: __('订单编号'), operate: 'LIKE'},
                        {field: 'product_name', title: __('产品名称'), operate: 'LIKE',operate: false},
                        {field: 'video_img', title: __('产品图片'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'address_tel', title: __('买家手机号'), operate: 'LIKE'},
                        {field: 'shop_tel', title: __('卖家手机号'), operate: 'LIKE'},
                        {field: 'price', title: __('订单金额'), operate: 'LIKE',operate: false},
                        {field: 'peisong', title: __('配送方式'), formatter: Table.api.formatter.status, searchList: {'1,0': '自提', '0,1': '快递'},operate: false},
                        {field: 'address_username', title: __('收货人'), operate: 'LIKE',formatter:function (value, row, index) {
                            if(row.ship_type == 1){
                                var sta = '';
                                if(row.address_username) sta += row.address_username;
                                if(row.address_tel) sta += row.address_tel;
                                if(row.address_province) sta += row.address_province;
                                if(row.address_city) sta += row.address_city;
                                if(row.address_district) sta += row.address_district;
                                return sta;
                            }else{
                                return '';
                            }
                        }},
                        {field: 'status', title: __('状态'), formatter: Table.api.formatter.status, searchList: {'1': '待支付', '2': '待发货','3': '待收货', '5': '已完成', '6': '已预约快递','-1': '已取消'}},
                        {field: 'add_time', title: __('创建时间'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                    ]
                ]
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});